//index.js
const haiguan_detail= require('../../config').haiguan_detail;
const getCurrentPagesApi = require('../../utils/common').getCurrentPagesApi;
var WxParse = require('../../wxParse/wxParse.js');
// var html = '';
//获取应用实例
const app = getApp()

Page({
  data: {
    content:[]

  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  shikuang_detail: function(e) {
    wx.navigateTo({
      url: `../shikuang_detail/shikuang_detail?id=${e.currentTarget.dataset.id}`
    })
  },
  onShareAppMessage: function (res) {
    if (res.from === 'menu') {
      return {
        title: '国数源',
        path: getCurrentPagesApi()
      }
    }
  },
  onLoad: function (params) {
    var id = params.id
    this.ready(id);
  },
  ready: function(id) {
    var that = this
    app.checkSession(function(res){
    // console.log(res);
      if(res){
           wx.request({
            url: haiguan_detail,
            data:{id: id},
            success: function(res){
              // console.log(res.data[0].content)
              // html = res.data[0].content
              if(app.globalData.userType >= res.data[0].vip){
                that.setData({
                  content: res.data
                }),
                WxParse.wxParse('article', 'html', res.data[0].content, that, 5)
              }else{
                 // 直接跳转提示用户成为会员页面
                 wx.navigateTo({
                   url: `../index/kaitonghuiyuan`
                 })
                app.globalData.huiyuanpath = getCurrentPagesApi()
              }
            },
            fail: function(data){
              console.log(data)
            }
          })
      }else{
        // console.log(app.globalData.userType)
        // console.log(345)
        if(app.globalData.userType){
          wx.request({
          url: haiguan_detail,
          data:{id: id},
          success: function(res){
              if(app.globalData.userType >= res.data[0].vip){
                  that.setData({
                    content: res.data
                  }),
                  WxParse.wxParse('article', 'html', res.data[0].content, that, 5)
                }else{
                   // 直接跳转提示用户成为会员页面
                   wx.navigateTo({
                     url: `../index/kaitonghuiyuan`
                   })
                  app.globalData.huiyuanpath = getCurrentPagesApi()
                }
              
           },
           fail: function(data){
              console.log(data)
            }
        })
      }else{
            wx.switchTab({
               url: `../user/user`
             })
          }
      }
    })
    
  },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  }
})
